package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.AgentUser;
import com.ruoyi.fly.domain.vo.AgentUserSearchVo;
import com.ruoyi.fly.domain.vo.AgentUserVo;
import com.ruoyi.fly.mapper.AgentUserMapper;
import com.ruoyi.fly.service.AgentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AgentUserServiceImpl implements AgentUserService {

    @Autowired
    private AgentUserMapper agentUserMapper;


    /**
     * 根据用户信息主键查询用户信息
     * @param userId 用户信息主键
     * @return
     */
    @Override
    public AgentUser selectSysUserByUserId(String userId) {
        return agentUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * @param sysUser 用户信息
     * @return
     */
    @Override
    public List<AgentUser> selectSysUserList(AgentUserSearchVo sysUser) {
        return agentUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户信息
     * @param sysUser 用户信息
     * @return
     */
    @Override
    public R insertSysUser(AgentUserVo sysUser) {
        for (Long roleId : sysUser.getRoleIds()) {
            if (roleId.toString().equals("2")) {
                sysUser.setAgentType("2");
            }
        }
        //sysUser.setCreateBy(Long.valueOf(String.valueOf(SecurityUtils.getUserId())));
        return agentUserMapper.insertSysUser(sysUser) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 修改用户信息
     * @param sysUser 用户信息
     * @return
     */
    @Override
    public R updateSysUser(AgentUserVo sysUser) {
        for (Long roleId : sysUser.getRoleIds()) {
            if (roleId.toString().equals("2")) {
                sysUser.setAgentType("2");
            }
        }
        //sysUser.setUpdateBy(SecurityUtils.getUserId());
        return agentUserMapper.updateSysUser(sysUser) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除用户信息
     * @param userId 用户信息主键
     * @return
     */
    @Override
    public R deleteSysUserByUserId(String userId) {
        return agentUserMapper.deleteSysUserByUserId(userId) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteSysUserByUserIds(String[] ids) {
        return agentUserMapper.deleteSysUserByUserIds(ids) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }


}
